<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>我的账号 已绑定修改</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="format-detection" content="telephone=no" />
  <jsp:include page="/jsp/inc_head.jsp"></jsp:include>
<style>
  #zhezhao{
    background:#333;
    bottom: 0;
	width:100%;
    display: none;
    left: 0;
    opacity: 0.9;
    position: fixed;
    right: 0;
    top: 0;
    z-index: 1000;
	}
	.zzcltext{
		font-size:18px;
		color:#fff;
		margin-top: 200px;
		text-align:center;
		font-family: "Segoe UI","Lucida Grande",Helvetica,Arial,"Microsoft YaHei",FreeSans,Arimo,"Droid Sans","wenquanyi micro hei","Hiragino Sans GB","Hiragino Sans GB W3",FontAwesome,sans-serif;
	}
</style>
</head>

<body style="padding-bottom:50px;">
<input id="type_code" type="hidden"/>
<input id="jsonstring_code" type="hidden"/>
<div class="am-g">
    <div class="am-u-sm-12 am-pad-none">
    	<div class="am-u-sm-12 wdzh_lie">
            <div class="am-u-sm-8 am-pad-none">
            	<input type="text" id="phone" value="" placeholder="请输入您需要绑定的手机号"/>
            </div>
        	<div class="am-u-sm-4 am-pad-none am-text-right">
            	<button id="Time"  onclick="getCodeHandler()"  class="yzmsg">获取验证码</button>
            </div>
<script>
        var ajaxpath="${pageContext.request.contextPath}";
        var sendMobile, sendCode;//保存发送的手机号和验证码
        var phtest = /^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$/;//手机格式验证


        //倒计时
        function TimeClock(){
             var Foamtime=true;
             var time=60;
            // var code=$(this); 
             if (Foamtime) {
                   Foamtime=false;
                //   code.addClass("yzmsg1");
                   $("#Time").addClass("yzmsg1");
                   var t=setInterval(function () {
                        time--;
                       // code.html("重发("+time+"s)");
                       $("#Time").html("重发("+time+"s)");
                        if (time == 0) {
                             clearInterval(t);
                             Foamtime=true;
                            // code.removeClass("yzmsg1");
                            $("#Time").removeClass("yzmsg1");
                        }
                   },1000);
             }
        }

        $(function(){
            var jsonstring_code=decodeURI(decodeURI(request("jsonstring")));
            var type_code=request("type");
            $("#type_code").val(type_code);
            $("#jsonstring_code").val(jsonstring_code);
        });

        //获取url参数值
        function request(paras){
            var url = location.href;
            var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
            var paraObj = {};
            for (i=0; j=paraString[i]; i++){
                paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
            }
            var returnValue = paraObj[paras.toLowerCase()];
            if(typeof(returnValue)=="undefined"){
                return "";
            }else{
                return returnValue;
            }
        }
        
        function getCodeHandler() {
		    var userId = $("#hidUserId").val();
			var phone = $("#phone").val();
			var hisphone = $("#hidphone").val();
			if (!phtest.test(phone)) {
				$("#alert_message").html("手机号格式错误");
				$("#my-alert").modal('open');   
				return;
			}
			if (phone==hisphone) {
			    var text="您的账户当前号码已为 "+hisphone+"，无需修改";
			    $("#alert_message").html(text);
				$("#my-alert").modal('open');   
				return;
			}
			var url_PhoneRegisted = ajaxpath+"/register/queryRegInfoByPhone.do?phone="+ phone+"&guid="+ new Date();	
			$.get(url_PhoneRegisted, function(data){
			    if(data.result=="nouser"){
			         TimeClock();
			         sendMsg(phone);
			    }else{
			         $("#alert_message").html("该号码已被注册");
					 $("#my-alert").modal('open'); 
			    }
			});			
		}
		//发送验证码
		function sendMsg(phone) {
		var userId = $("#hidUserId").val();
			var url_send = ajaxpath+"/basic/sendMsg.do?mobile=" + phone + "&guid="+ new Date();
			
			$.get(url_send, function(data) {
				sendMobile = data.phone;
				sendCode = data.code;
				$("#alert_message_1").html("验证码已发送,请注意查收！");
				$("#my-alert-1").modal('open');
			});
		} 
		
		function submitHander(){
		    var code = $("#code").val();
			var phone = $("#phone").val(); 
			
			if (!phtest.test(phone)) {
				$("#alert_message").html("手机号格式错误");
				$("#my-alert").modal('open');   
				$("#phone").focus();
				return;	
			}
			if (!code) {
				$("#alert_message").html("手机验证码不可为空");
				$("#my-alert").modal('open');  					
				$("#code").focus();
				return;
			}
			if(sendMobile!=phone||sendCode!=code){
			    $("#alert_message").html("手机号或验证码错误");
				$("#my-alert").modal('open');   
				return;
			}
			$("#zhezhao").css("display","block");//遮罩层
			var url_submit = ajaxpath+"/register/updateRegister.do?phone=" + phone+"&guid="+ new Date();
			$.get(url_submit, function(data) {
			      if (data.result == "success") {
					  $("#alert_message_1").html("修改成功！");
					  $("#my-alert-1").modal('open');
				      //window.location.href = ajaxpath+"/client_toAccountchangesuccess.do?guid="+ new Date();

                      toUrl();
			      } else {
				      /* $("#alert_message").html(data.msg);
				      $("#my-alert").modal('open');
				      $("#zhezhao").css("display","none");//遮罩层 */
				      window.location.href = ajaxpath+"/client_toAccountchangefail.do?guid="+ new Date();
			      }
			});
		}


        function toUrl(){
            var type_code= $("#type_code").val();
            var jsonstring_code=$("#jsonstring_code").val();
            if(type_code=='1')//从立即购买跳转至注册，现在需要回跳到去支付页面
            {
                var json_date= JSON.parse(jsonstring_code);
                //加购物车，跳转去支付页面
                $.ajax({
                    type : "POST",
                    url : ajaxpath + "/shop/insertMycart.do",
                    data : json_date,// 你的formid
                    async : false,
                    success : function(data) {
                        if (data.result == "success") {
                            window.location.href = ajaxpath	+ "/order/toOrderConfirm.do?type=1&cartid=" + data.cartid;
                        } else {
                            window.location.href = ajaxpath+"/client_toClientCenter.do?guid="+ new Date();
                        }
                        ;
                    }
                });
            }else if(type_code=='2'){
                //返回到当时跳转的页面,商品详情页
                var json_date= JSON.parse(jsonstring_code);
                var goodsid =getJson(json_date,'goodsid');
                var stype=getJson(json_date,'stype');
                window.location.href = ajaxpath+"/client_toGoodsdetail.do?goodsid="+goodsid+"&stype="+stype;
            } else
            {
                window.location.href = ajaxpath+"/client_toClientCenter.do?guid="+ new Date();
            }

        }

        function getJson(jsonObj,key){
            return jsonObj[''+key+''];
        }
		
		
</script>
        </div>
    	<div class="am-u-sm-12 wdzh_lie">
            <div class="am-u-sm-12 am-pad-none">
            	<input id="code" type="text" value="" placeholder="请输入验证码"/>
            </div>
        </div>
    </div>
</div>
<div onclick="submitHander();" class="am-g btm_fixed">
	<div class="am-u-sm-12 am-text-center">
    	下一步
    </div>
</div>

<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
	<div class="am-modal-dialog">
		<div class="am-modal-hd" id="alert_title">哎呦喂，出错啦</div>
		<div class="am-modal-bd" id="alert_message"></div>
		<div class="am-modal-footer">
			  <span class="am-modal-btn">确定</span>
		</div>
	</div>
</div>
<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert-1">
	<div class="am-modal-dialog">
		<div class="am-modal-hd" id="alert_title_1">提示</div>
		<div class="am-modal-bd" id="alert_message_1"></div>
		<div class="am-modal-footer">
			  <span class="am-modal-btn">确定</span>
		</div>
	</div>
</div>
<div id="zhezhao">
	<div class="zzcltext">
	         正在处理，请稍等。。。。
	</div>
</div>

<input type="hidden" id="hidphone" value="${phone}" />
</body>
</html>
